﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Clinet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElectronicShoppingMall.Service.Services.Client
{
    public class ClientUserService : IDisposable
    {
        private ElectronicShoppingMallContext data;
        public ClientUserService()
        {
            data = new ElectronicShoppingMallContext();
        }
        /// <summary>
        /// 顾客登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ClientUserModel Login(string account, string password)
        {
            ClientUserModel result;
            var user = data.Users.SingleOrDefault(u => u.Account == account && u.Password == password);
            if (user == null)
            {
                throw new Exception("用户名或密码错误");
            }

            result = new ClientUserModel()
            {
                Account = user.Account,
                Password = user.Password,
                Name = user.NickName,
                UserId = user.UserId
            };
            return result;
        }
        /// <summary>
        /// 获取顾客登录信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ClientUserModel GetUserModel(int userId)
        {
            var user = data.Users.SingleOrDefault(u => u.UserId == userId);
            return new ClientUserModel()
            {
                UserId = user.UserId,
                Account = user.Account,
                Name = user.NickName
            };
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="userInfo"></param>
        public void ChangeUserInfo(int userId, UserInfoModel userInfo)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var user = data.Users.SingleOrDefault(u => u.UserId == client.UserId);
            user.PhoneNumber = userInfo.PhoneNumber;
            user.Email = userInfo.Email;
            user.Birthday = Convert.ToDateTime(userInfo.Birthday.Replace("GMT+0800 (中国标准时间)", ""));

            user.NickName = userInfo.NickName;
            user.Gender = Boolean.Parse(userInfo.Gender);
            data.SaveChanges();
        }
        /// <summary>
        /// 获取用户地址簿
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<DeliveryInfo> GetUserDeliveryInfo(int userId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            return client.DeliveryInfoes.ToList();
        }
        /// <summary>
        /// 将地址设为默认
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="deliverId"></param>
        public void SetDeliveryDefault(int userId, int deliverId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var oldDefaultDeliver = data.DeliveryInfoes.SingleOrDefault(d => d.ClientId == client.ClientId && d.IsDefault == true);
            if (oldDefaultDeliver != null)
            {
                oldDefaultDeliver.IsDefault = false;
            }
            var deliver = data.DeliveryInfoes.SingleOrDefault(d => d.ClientId == client.ClientId && d.Id == deliverId);
            deliver.IsDefault = true;
            data.SaveChanges();
        }
        /// <summary>
        /// 添加收货地址
        /// </summary>
        /// <param name="consignee"></param>
        /// <param name="detailedAddress"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="userId"></param>
        public void AddDelivertInfo(string consignee, string detailedAddress, string phoneNumber, int userId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            DeliveryInfo deliver = new DeliveryInfo
            {
                ClientId = client.ClientId,
                DetailedAddress = detailedAddress,
                Consignee = consignee,
                IsDefault = false,
                PhoneNumber = phoneNumber
            };
            data.DeliveryInfoes.Add(deliver);
            data.SaveChanges();
        }
        /// <summary>
        /// 删除收货地址
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="deliverId"></param>
        public void DeleteDeliveryInfo(int userId, int deliverId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var deliver = data.DeliveryInfoes.SingleOrDefault(d => d.ClientId == client.ClientId && d.Id == deliverId);
            if (deliver.IsDefault)
            {
                data.DeliveryInfoes.Remove(deliver);
                data.SaveChanges();

                var clientDeliver = data.DeliveryInfoes.Where(d => d.ClientId == client.ClientId).FirstOrDefault();
                if (clientDeliver != null)
                {
                    clientDeliver.IsDefault = true;
                }
                data.SaveChanges();


            }
            else
            {
                data.DeliveryInfoes.Remove(deliver);
                data.SaveChanges();
            }
        }
       /// <summary>
       /// 用户注册
       /// </summary>
       /// <param name="userInfo"></param>
       /// <returns></returns>
        public string ClientRegister(UserInfoModel userInfo)
        {
            User user = new User
            {
                Account = userInfo.Account,
                UserAvatar = "",
                Birthday = Convert.ToDateTime(userInfo.Birthday.Replace("GMT+0800 (中国标准时间)", "")),
                CreateTime = DateTime.Now,
                Email = userInfo.Email,
                Gender = Boolean.Parse(userInfo.Gender),
                NickName = userInfo.NickName,
                PhoneNumber = userInfo.PhoneNumber,
                Password = userInfo.Password
            };
            data.Users.Add(user);
            data.SaveChanges();
            Data.Client client = new Data.Client
            {
                UserId = user.UserId
            };
            data.Clients.Add(client);
            data.SaveChanges();
            return "注册成功";
        }
       /// <summary>
       /// 修改密码
       /// </summary>
       /// <param name="userId"></param>
       /// <param name="oldPassword"></param>
       /// <param name="password"></param>
       /// <returns></returns>
        public string ModifyPassword(int userId, string oldPassword, string password)
        {
            var user = data.Users.SingleOrDefault(u => u.UserId == userId);
            if(user.Password == oldPassword)
            {
                user.Password = password;
                data.SaveChanges();
                return "修改成功";
            }
            else
            {
                return "原密码不正确";
            }
        }

        public void Dispose()
        {
            data.Dispose();
        }
    }
}
